CosineSimilarity reward
CosineSimilarity
Bases: RewardFunctionWrapper
Embedding similarity; use for free-form text, not exact or numeric answers.
Formula:
The formula is similar to the classic cosine similarity used in deep learning, but scaled to [0.0, 1.0] and adjusted to have a reward that tend towards 1.0 if the two objects are similar (and 0.0 otherwise).
Example:
program.compile(
reward=synalinks.rewards.CosineSimilarity(
embedding_model=embedding_model
),
optimizer=synalinks.optimizers.RandomFewShot(),
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding_model
|
EmbeddingModel
|
The embedding model to use to compute the cosine similarity. |
None
|
axis
|
int
|
(Optional) Defaults to |
-1
|
name
|
str
|
(Optional) string name of the reward instance. |
'cosine_similarity'
|
in_mask
|
list
|
(Optional) list of keys to keep to compute the reward. |
None
|
out_mask
|
list
|
(Optional) list of keys to remove to compute the reward. |
None
|
in_mask_pattern
|
str
|
Optional. Regex pattern; fields whose names match
are kept (combined with |
None
|
out_mask_pattern
|
str
|
Optional. Regex pattern; fields whose names match
are dropped (combined with |
None
|
Source code in synalinks/src/rewards/cosine_similarity.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
cosine_similarity(y_true, y_pred, embedding_model=None, axis=-1)
async
Embedding similarity; use for free-form text, not exact or numeric answers.
Formula:
The formula is similar to the classic cosine similarity used in deep learning, but scaled to [0.0, 1.0] and adjusted to have a reward that tend towards 1.0 if the two objects are similar (and 0.0 otherwise).
Each top-level field of y_true is embedded as one text (a list or nested
field included) and compared with the same field of y_pred; the reward
is the mean over the fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
JsonDataModel
|
The ground truth JSON data_model. |
required |
y_pred
|
JsonDataModel
|
The predicted JSON data_model. |
required |
embedding_model
|
EmbeddingModel
|
The embedding model to use to compute the cosine similarity. |
None
|
axis
|
int
|
(Optional) Defaults to |
-1
|
Returns:
| Type | Description |
|---|---|
float
|
The reward value, which tend to 1.0 if the values are similar, and towards 0.0 otherwise. |